package com.ophiux.FeverManagement.aplication.serviceImpl.doctor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ophiux.FeverManagement.aplication.bean.doRegisterDoctorRequest;
import com.ophiux.FeverManagement.aplication.bean.doRegisterDoctorResponse;
import com.ophiux.FeverManagement.aplication.service.doctor.doRegisterDoctorService;
import com.ophiux.FeverManagement.domain.entity.Authotity;
import com.ophiux.FeverManagement.domain.entity.DoctorInfo;
import com.ophiux.FeverManagement.domain.entity.RbDoctorThirdparty;
import com.ophiux.FeverManagement.domain.repository.IAuthotityRepositories;
import com.ophiux.FeverManagement.domain.repository.IDoctorRepositories;
import com.ophiux.FeverManagement.domain.repository.IDoctorThirdpartyRepositories;
import com.ophiux.micro.core.application.Result;
import com.ophiux.micro.core.application.ResultEnum;
import com.ophiux.utils.common.DateUtil;
import com.ophiux.utils.common.HanyuPinyinHelper;
import com.ophiux.utils.encrypt.OphiuxEncrypt;
@Service("doRegisterDoctorService")
public class doRegisterDoctorServiceImpl implements doRegisterDoctorService{
	@Autowired
	IDoctorRepositories doctorRepositories;//医生信息仓储
	@Autowired
	IDoctorThirdpartyRepositories doctorThirdpartyRepositories;//医生第三方绑定仓储
	@Autowired
	IAuthotityRepositories authotityRepositories;//权限信息仓储
	
	@Override
	@Transactional
	public Result<doRegisterDoctorResponse> doRegisterDoctor(doRegisterDoctorRequest dto) {
		Result<doRegisterDoctorResponse> result = new Result<>();
		if(!dto.isParamValid()) {
			return Result.fail(ResultEnum.requestParamFail);
		}
		try {
			String Phone = dto.getPHONE();
			DoctorInfo queryDoctor = doctorRepositories.getDoctorByPhone(Phone);
			if(queryDoctor != null) {
				return Result.fail(ResultEnum.userAgin);
			}
			
			/**
			 * 入库医生信息
			 */
			DoctorInfo doctor = new DoctorInfo();
			doctor.setUSER_NAME(dto.getPHONE());
			doctor.setPASSWORD(OphiuxEncrypt.encryptPassWord(dto.getPASSWORD()));
			doctor.setORG_ID("20008");
			doctor.setDEPT_ID("20048");
			HanyuPinyinHelper pinyinHelper = new HanyuPinyinHelper();
			String pinYin = pinyinHelper.toHanyuPinyin(dto.getSTAFF_NAME());
			doctor.setPINYIN_CODE(pinYin);
			doctor.setID_CARD_NUMBER(dto.getID_CARD_NUMBER());
			doctor.setEMAIL(dto.getEMAIL());
			doctor.setSTAFF_NAME(dto.getSTAFF_NAME());
			doctor.setPHONE(dto.getPHONE());
			doctor.setCREATE_DATE(DateUtil.getCurDateStr());
			String doctorId = doctorRepositories.save(doctor);
			
			/**
			 * 入库第三方绑定信息
			 */
			RbDoctorThirdparty doctorThirdparty = new RbDoctorThirdparty();
			doctorThirdparty.setDOCTOR_ID(doctorId);
			doctorThirdparty.setCREATE_TIME(DateUtil.getCurDateStr());
			doctorThirdparty.setTHIRDPARTY_ID(dto.getUSER_BIND().getOTHER_ID());
			doctorThirdparty.setTYPE(dto.getUSER_BIND().getTYPE());
			
			doctorThirdpartyRepositories.save(doctorThirdparty);
			
			/**
			 * 入库默认的权限信息
			 */
			Authotity authotity = new Authotity();
			authotity.setDEPT_ID("20048");
			authotity.setUSER_ID(doctorId);
			authotityRepositories.insert_default_authotity(authotity);
			
			result = Result.success();
		} catch (Exception e) {
			e.printStackTrace();
			result = Result.fail("","操作异常");
		}
		
		return result;
	}

}
